# -*- makefile -*-
# definitions used in more than one Makefile / rules file

# common vars
SHELL		= /bin/bash -e	# brace expansion used in rules file
PWD		:= $(shell pwd)
srcdir		= $(PWD)/src
builddir	= $(PWD)/build
stampdir	= stamps

# creates {srcdir,builddir}_{hppa64,ia6432,spu}
$(foreach x,srcdir builddir,$(foreach target,hppa64 ia6432 spu neon,$(eval \
  $(x)_$(target) := $($(x))-$(target))))

# for architecture dependent variables and changelog vars
vafilt = $(subst $(2)=,,$(filter $(2)=%,$(1)))
# for rules.sonames
vafilt_defined = 1

DPKG_VARS		:= $(shell dpkg-architecture)
DEB_BUILD_GNU_TYPE	?= $(call vafilt,$(DPKG_VARS),DEB_BUILD_GNU_TYPE)
DEB_HOST_ARCH		?= $(call vafilt,$(DPKG_VARS),DEB_HOST_ARCH)
DEB_HOST_GNU_CPU	?= $(call vafilt,$(DPKG_VARS),DEB_HOST_GNU_CPU)
DEB_HOST_GNU_SYSTEM	?= $(call vafilt,$(DPKG_VARS),DEB_HOST_GNU_SYSTEM)
DEB_HOST_GNU_TYPE	?= $(call vafilt,$(DPKG_VARS),DEB_HOST_GNU_TYPE)

CHANGELOG_VARS := $(shell dpkg-parsechangelog | \
	sed -n 's/ /_/g;/^[^_]/s/^\([^:]*\):_\(.*\)/\1=\2/p')

# the name of the source package
PKGSOURCE := $(call vafilt,$(CHANGELOG_VARS),Source)
# those are required here too
SOURCE_VERSION := $(call vafilt,$(CHANGELOG_VARS),Version)
DEB_VERSION := $(strip $(shell echo $(SOURCE_VERSION) | \
        sed -e 's/.*://' -e 's/ds[0-9]*//'))
# epoch used for gcc versions up to 3.3.x, now used for some remaining
# libraries: libgcc1, libobjc1
EPOCH := 1
DEB_EVERSION := $(EPOCH):$(DEB_VERSION)
BASE_VERSION := $(shell echo $(DEB_VERSION) | sed -e 's/\([1-9]\.[0-9]\).*-.*/\1/')

# ---------------------------------------------------------------------------
# set target
# - GNU triplet via DEB_TARGET_GNU_TYPE
# - Debian arch in debian/target
# - Debian arch via DEB_GCC_TARGET or GCC_TARGET
#
# alias
ifdef GCC_TARGET
  DEB_GCC_TARGET := $(GCC_TARGET)
endif
ifdef DEB_TARGET_GNU_TYPE
  TARGET_VARS := $(shell dpkg-architecture -f -t$(DEB_TARGET_GNU_TYPE) 2>/dev/null)
else
  # allow debian/target to be used instead of DEB_GCC_TARGET - this was requested
  # by toolchain-source maintainer
  DEBIAN_TARGET_FILE := $(strip $(if $(wildcard debian/target),$(shell cat debian/target 2>/dev/null)))
  ifndef DEB_TARGET_ARCH
    ifneq (,$(DEBIAN_TARGET_FILE))
      DEB_TARGET_ARCH := $(DEBIAN_TARGET_FILE)
    else
      ifdef DEB_GCC_TARGET
        DEB_TARGET_ARCH := $(DEB_GCC_TARGET)
      else
        DEB_TARGET_ARCH := $(DEB_HOST_ARCH)
      endif
    endif
  endif
  TARGET_VARS := $(shell dpkg-architecture -f -a$(DEB_TARGET_ARCH) 2>/dev/null)
endif

DEB_TARGET_ARCH		?= $(call vafilt,$(TARGET_VARS),DEB_HOST_ARCH)
DEB_TARGET_ARCH_OS	?= $(call vafilt,$(TARGET_VARS),DEB_HOST_ARCH_OS)
DEB_TARGET_ARCH_CPU	?= $(call vafilt,$(TARGET_VARS),DEB_HOST_ARCH_CPU)
DEB_TARGET_GNU_CPU	?= $(call vafilt,$(TARGET_VARS),DEB_HOST_GNU_CPU)
DEB_TARGET_GNU_TYPE	?= $(call vafilt,$(TARGET_VARS),DEB_HOST_GNU_TYPE)
DEB_TARGET_GNU_SYSTEM	?= $(call vafilt,$(TARGET_VARS),DEB_HOST_GNU_SYSTEM)

ifeq ($(DEB_TARGET_ARCH),)
  $(error Invalid architecure.)
endif

# including unversiond symlinks for binaries
#with_unversioned = yes

# ---------------------------------------------------------------------------
# cross-compiler config
# - typical cross-compiler
# - reverse cross (built to run on the target)
# - full canadian
# - native
#
# build != host && host == target : reverse cross (REVERSE_CROSS == yes)
# build == host && host != target : typical cross (DEB_CROSS == yes)
# build != host && host != target : canadian (DEB_CROSS == yes)
# build == host && host == target : native
ifneq ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE))
  ifneq ($(DEB_HOST_GNU_TYPE),$(DEB_TARGET_GNU_TYPE))
    DEB_CROSS = yes
  else
    REVERSE_CROSS = yes
  endif
else
  ifneq ($(DEB_HOST_GNU_TYPE),$(DEB_TARGET_GNU_TYPE))
    DEB_CROSS = yes
  endif
endif

# ---------------------------------------------------------------------------
# cross compiler support
ifeq ($(DEB_CROSS),yes)
  # TARGET: Alias to DEB_TARGET_ARCH (Debian arch name)
  # TP: Target Prefix. Used primarily as a prefix for cross tool
  #     names (e.g. powerpc-linux-gcc).
  # TS: Target Suffix. Used primarily at the end of cross compiler
  #     package names (e.g. gcc-powerpc).
  # LS: Library Suffix. Used primarily at the end of cross compiler
  #     library package names (e.g. libgcc-powerpc-cross).
  DEB_TARGET_ALIAS ?= $(DEB_TARGET_GNU_TYPE)
  TARGET := $(DEB_TARGET_ARCH)
  TP :=  $(subst _,-,$(DEB_TARGET_GNU_TYPE))-
  TS := -$(subst _,-,$(DEB_TARGET_ALIAS))
  LS := -$(subst _,-,$(DEB_TARGET_ARCH))-cross

  cross_bin_arch := -$(subst _,-,$(DEB_TARGET_ALIAS))
  cross_lib_arch := -$(subst _,-,$(DEB_TARGET_ARCH))-cross

  TARGET_ALIAS := $(DEB_TARGET_ALIAS)
else
  TARGET_ALIAS := $(DEB_TARGET_GNU_TYPE)

  ifeq ($(TARGET_ALIAS),i386-gnu)
    TARGET_ALIAS := i586-gnu
  endif

  #ifeq ($(TARGET_ALIAS),i486-linux-gnu)
  #  TARGET_ALIAS := i686-linux-gnu
  #endif

  TARGET_ALIAS := $(subst i386,i486,$(TARGET_ALIAS))

  # configure as linux-gnu, not linux
  #ifeq ($(findstring linux,$(TARGET_ALIAS))/$(findstring linux-gnu,$(TARGET_ALIAS)),linux/)
  #  TARGET_ALIAS := $(TARGET_ALIAS)-gnu
  #endif

  # configure as linux, not linux-gnu
  #TARGET_ALIAS := $(subst linux-gnu,linux,$(TARGET_ALIAS))
endif

printarch:
	@echo DEB_TARGET_ARCH: $(DEB_TARGET_ARCH)
	@echo DEB_TARGET_ARCH_OS: $(DEB_TARGET_ARCH_OS)
	@echo DEB_TARGET_ARCH_CPU: $(DEB_TARGET_ARCH_CPU)
	@echo DEB_TARGET_GNU_SYSTEM: $(DEB_TARGET_GNU_SYSTEM)
	@echo TARGET_ALIAS: $(TARGET_ALIAS)
	@echo TP: $(TP)
	@echo TS: $(TS)

# -------------------------------------------------------------------
# bootstrap options
ifdef WITH_BOOTSTRAP
  # "yes" is the default and causes a 3-stage bootstrap.
  # "no" means to just build the first stage, and not create the stage1
  # directory.
  # "lean" means a lean 3-stage bootstrap, i.e. delete each stage when no
  # longer needed.
  with_bootstrap = $(WITH_BOOTSTRAP)
endif

# -------------------------------------------------------------------
# sysroot options
ifdef WITH_SYSROOT
  with_sysroot = $(WITH_SYSROOT)
endif
ifdef WITH_BUILD_SYSROOT
  with_build_sysroot = $(WITH_BUILD_SYSROOT)
endif

# -------------------------------------------------------------------
# for components configuration

COMMA = ,
SPACE = $(EMPTY) $(EMPTY)

# lang= overwrites all of nolang=, overwrites all of WITHOUT_LANG

DEB_LANG_OPT	:= $(filter lang=%,$(DEB_BUILD_OPTIONS))
DEB_LANG	:= $(strip $(subst $(COMMA), ,$(patsubst lang=%,%,$(DEB_LANG_OPT))))
DEB_NOLANG_OPT	:= $(filter nolang=%,$(DEB_BUILD_OPTIONS))
DEB_NOLANG	:= $(strip $(subst $(COMMA), ,$(patsubst nolang=%,%,$(DEB_NOLANG_OPT))))
lfilt	= $(strip $(if $(DEB_LANG), \
		$(if $(filter $(1) $(2),$(DEB_LANG)),yes),$(3)))
nlfilt	= $(strip $(if $(DEB_NOLANG), \
		$(if $(filter $(1) $(2),$(DEB_NOLANG)),disabled by $(DEB_NOLANG_OPT),$(3))))
wlfilt	= $(strip $(if $(filter $(1) $(2), $(subst $(COMMA), ,$(WITHOUT_LANG))), \
		disabled by WITHOUT_LANG=$(WITHOUT_LANG),$(3)))
envfilt	= $(strip $(or $(call lfilt,$(1),$(2)),$(call nlfilt,$(1),$(3)),$(call wlfilt,$(1),$(3)),$(4)))

# -------------------------------------------------------------------
# architecture specific config

# FIXME: lsb_release triggers a kernel bug on the Debian mips/mipsel buildds
#ifneq (,$(filter $(DEB_TARGET_ARCH),mips mipsel))
#  distribution	:= Debian
#else
#  distribution	:= $(shell lsb_release -is)
#endif

distribution := maemo

ifeq ($(distribution),Ubuntu)
  with_arm_thumb := yes
endif

# -------------------------------------------------------------------
# basic config

# common things ---------------
# build common packages, where package names don't differ in different
# gcc versions (protoize, fixincludes, libgcj-common) ...
with_common_pkgs := yes
# ... and some libraries, which do not change (libgcc1, libmudflap, libssp0).
with_common_libs := yes
# XXX: should with_common_libs be "yes" only if this is the default compiler
# version on the targeted arch?

# build a -base package.
ifneq ($(DEB_CROSS),yes)
  with_gccbase := yes
endif
# build dev packages.
with_dev := yes

with_cpp := yes

# set lang when built from a different source package.
separate_lang := no

# separate -base package for the cross compiler.
ifeq ($(DEB_CROSS),yes)
  with_gccxbase := yes
endif

ifneq ($(PKGSOURCE),gcc-snapshot)
  # --program-suffix=-$(BASE_VERSION)
  versioned_packages := yes
  ifneq ($(DEB_CROSS),yes)
    with_common_gcclibdir := yes
  endif
else
  # for control.in
  gcc_snapshot := yes
endif

#no_dummy_cpus := ia64 i386 hppa s390 sparc
#ifneq (,$(filter $(DEB_TARGET_ARCH_CPU),$(no_dummy_cpus)))
#  with_base_only	:= no
#  with_common_libs	:= yes
#  with_common_pkgs	:= yes
#else
#  with_base_only	:= yes
#  with_common_libs	:= no
#  with_common_pkgs	:= no
#  with_dev		:= no
#endif

ifeq ($(versioned_packages),yes)
  pkg_ver := -$(BASE_VERSION)
  PV := $(pkg_ver)
endif

# -------------------------------------------------------------------
# configure languages

# C ---------------------------
enabled_languages := c

# Build all packages needed for C development
ifneq ($(with_base_only),yes)
  ifeq ($(with_dev),yes)
    with_cdev := yes
  endif
endif

# Ada --------------------
ada_no_cpus	:= m32r m68k sh3 sh3eb sh4 sh4eb
ada_no_systems	:= gnu knetbsd-gnu
ada_no_cross	:= yes
ada_no_snap	:= no

ifeq ($(with_dev),yes)
  ifneq ($(separate_lang),yes)
    with_ada := yes
  endif
endif
ifneq (,$(findstring $(DEB_TARGET_ARCH_CPU),$(ada_no_cpus)))
  with_ada := disabled for cpu $(DEB_TARGET_ARCH_CPU)
endif
ifneq (,$(findstring $(DEB_TARGET_GNU_SYSTEM),$(ada_no_systems)))
  with_ada := disabled for system $(DEB_TARGET_GNU_SYSTEM)
endif
ifeq ($(ada_no_cross)-$(DEB_CROSS),yes-yes)
  with_ada := disabled for cross compiler package
endif
ifeq ($(ada_no_snap)-$(PKGSOURCE),yes-gcc-snapshot)
  with_ada := disabled for snapshot build
endif
with_ada := $(call envfilt, ada, , , $(with_ada))

ifneq ($(PKGSOURCE),gcc-snapshot)
  with_separate_gnat := yes
endif
ifeq ($(with_ada)-$(with_separate_gnat),yes-yes)
  ifneq (,$(findstring gnat,$(PKGSOURCE)))
    languages := c
    separate_lang := yes
  else
    debian_extra_langs += ada
    with_ada := built from separate source
    with_libgnat := built from separate source
  endif
endif

with_ada := disabled for maemo

ifeq ($(with_ada),yes)
  enabled_languages += ada
  with_libgnat := yes
  # There are two exception handling mechanisms: ZCX (Zero-Cost
  # eXceptions) and SJLJ (setjump/longjump), selected and supported by
  # libgnat.  Thus we build both versions of libgnat on architectures
  # that support both (see ada-sjlj.diff).  Most cpus support both
  # mechanisms; here, we declare the few that support only one.
  libgnat_zcx_only_cpus :=
  libgnat_sjlj_only_cpus := arm armel
  ifneq (,$(filter $(DEB_TARGET_ARCH_CPU),$(libgnat_sjlj_only_cpus)))
    with_gnat_zcx := no
  else
    with_gnat_zcx := yes
  endif
  ifneq (,$(filter $(DEB_TARGET_ARCH_CPU),$(libgnat_zcx_only_cpus)))
    with_gnat_sjlj := no
  else
    with_gnat_sjlj := yes
  endif
  ifeq ($(with_gnat_zcx)-$(with_gnat_sjlj),no-no)
    # TODO: support cpus that do not support exceptions at all,
    # perhaps by building a restricted runtime library?  For now, flag
    # this as a packaging error.
    $(error this target supports neither ZCX nor SJLJ)
  endif
endif

# Pascal ----------------------
pascal_no_cross := yes
pascal_no_snap := yes

ifneq ($(separate_lang),yes)
  with_pascal := yes
endif
ifeq ($(pascal_no_cross)-$(DEB_CROSS),yes-yes)
  with_pascal := diasbled for cross compiler package
endif
ifeq ($(pascal_no_snap)-$(PKGSOURCE),yes-gcc-snapshot)
  with_pascal := disabled for snapshot build
endif
with_pascal := not yet ported to GCC 4.4
with_pascal := $(call envfilt, pascal, , , $(with_pascal))

ifneq ($(PKGSOURCE),gcc-snapshot)
  with_separate_gpc := yes
endif
ifeq ($(with_pascal)-$(with_separate_gpc),yes-yes)
  ifneq (,$(findstring gpc,$(PKGSOURCE)))
    languages := c
    separate_lang := yes
  else
    debian_extra_langs += pascal
    with_pascal := built from separate source
  endif
endif

ifneq ($(with_dev),yes)
  with_pascal := no
endif

with_pascal := disabled for maemo

ifeq ($(with_pascal),yes)
  with_gpidump := yes
  gpidump_no_cpus := mips mipsel
  ifneq (,$(findstring $(DEB_TARGET_ARCH_CPU),$(gpidump_no_cpus)))
    with_gpidump := disabled for cpu $(DEB_TARGET_ARCH_CPU)
  endif
  pascal_version := 20030830
  enabled_languages += pascal
endif

# C++ -------------------------
cxx_no_cpus := avr
ifneq ($(with_base_only),yes)
  ifneq ($(separate_lang),yes)
    with_cxx := yes
  endif
endif
ifneq (,$(findstring $(DEB_TARGET_ARCH_CPU),$(cxx_no_cpus)))
  with_cxx := disabled for cpu $(DEB_TARGET_ARCH_CPU)
endif
with_cxx := $(call envfilt, c++, obj-c++ java, , $(with_cxx))

# Build all packages needed for C++ development
ifeq ($(with_cxx),yes)
  ifeq ($(with_dev),yes)
    with_cxxdev := yes
    with_libcxxdbg := yes
  endif
  ifeq ($(with_common_pkgs),yes)
    with_libcxx := yes
  endif

  # debugging versions of libstdc++
  ifeq ($(with_cxxdev),yes)
    with_debug := yes
    debug_no_cpus :=
    ifneq (,$(findstring $(DEB_TARGET_ARCH_CPU),$(debug_no_cpus)))
      with_debug := disabled for cpu $(DEB_TARGET_GNU_CPU)
    endif
  endif
  with_debug := $(call envfilt, debug, , , $(with_debug))

  enabled_languages += c++
endif

# Java --------------------
# - To build a standalone gcj package (with no corresponding gcc
#   package): with_separate_libgcj=yes, with_standalone_gcj=yes
# - To build the java packages from the gcc source package:
#   with_separate_libgcj=no, with_standalone_gcj=no
# - To build gcc and java from separate sources:
#   with_separate_libgcj=yes, with_standalone_gcj=no

java_no_cpus := # mips mipsel
java_no_systems := knetbsd-gnu
java_no_cross := yes

ifneq ($(PKGSOURCE),gcc-snapshot)
  with_separate_libgcj := yes
endif
with_standalone_gcj := no

ifneq ($(separate_lang),yes)
  with_java := yes
endif

# java converted for V3 C++ ABI for some archs
ifeq ($(with_base_only),yes)
  with_java := no
endif
ifneq (,$(findstring $(DEB_TARGET_ARCH_CPU),$(java_no_cpus)))
  with_java := disabled for cpu $(DEB_TARGET_ARCH_CPU)
endif
ifneq (,$(filter $(DEB_TARGET_GNU_SYSTEM),$(java_no_systems)))
  with_java := disabled for system $(DEB_TARGET_GNU_SYSTEM)
endif
ifeq ($(java_no_cross)-$(DEB_CROSS),yes-yes)
  with_java := diasbled for cross compiler package
endif
with_java := $(call envfilt, java, , c++, $(with_java))

ifeq ($(with_java)-$(with_separate_libgcj),yes-yes)
  ifneq (,$(findstring gcj, $(PKGSOURCE)))
    languages := c c++
    separate_lang := yes
  else
    debian_extra_langs += java
    with_java := built from separate source
    with_gcj := built from separate source
    with_libgcj := buit from separate source
  endif
endif

with_java_plugin := no

with_java := disabled for maemo

ifeq ($(with_java),yes)
  # use the same names as OpenJDK
  java_cpu_map = armel=arm hppa=parisc i686=i386 i586=i386 i486=i386 mipsel=mips powerpc=ppc sh4=sh
  java_cpu = $(patsubst $(DEB_TARGET_ARCH_CPU)=%,%, \
                         $(filter $(DEB_TARGET_ARCH_CPU)=%,$(java_cpu_map)))
  ifeq (,$(java_cpu))
    java_cpu = $(DEB_TARGET_ARCH_CPU)
  endif
  java_priority = 10$(subst .,,$(BASE_VERSION))

  with_libgcj := yes
  with_libgcjbc := no

  ifneq (,$(findstring gcj-4,$(PKGSOURCE)))
    ifneq (,$(filter $(DEB_TARGET_ARCH), arm))
      with_gcj_base_only := yes
    endif
  endif

  #ifneq (,$(filter $(DEB_TARGET_ARCH),hppa))
  #  with_native_ecj := yes
  #endif

  with_java_maintainer_mode := no

  # used as well in debian/rules.conf to determine the build deps
  java_awt_peers = gtk # qt # xlib

  ifeq ($(with_common_libs),yes)
    with_libgcj_doc := yes
  endif

  # Build all packages needed for Java development (gcj, libgcj-dev)
  ifeq ($(with_dev),yes)
    with_javadev := yes
    with_gcj := yes
  endif

  with_java_alsa := yes
  ifeq (,$(filter $(DEB_TARGET_GNU_SYSTEM),linux-gnu))
    with_java_alsa := no
  endif

  enabled_languages += java
endif

# D ---------------------------
d_no_cross := yes
d_no_snap := yes

ifneq ($(PKGSOURCE),gcc-snapshot)
  with_separate_gdc := yes
endif

ifneq ($(separate_lang),yes)
  with_d := yes
endif
ifeq ($(d_no_snap)-$(PKGSOURCE),yes-gcc-snapshot)
  with_d := disabled for snapshot build
endif
with_d := not yet ported to GCC 4.4

ifeq ($(with_d)-$(with_separate_gdc),yes-yes)
  ifneq (,$(findstring gdc,$(PKGSOURCE)))
    languages := c c++
    separate_lang := yes
  else
    debian_extra_langs += d
    with_d := built from separate source
  endif
endif

ifeq ($(with_base_only),yes)
  with_d := no
endif

with_d := disabled for maemo

ifeq ($(with_d),yes)
  # no suffix for D 1.0
  libphobos_version :=
  # still experimental
  #libphobos_version := 2

  with_libphobos := yes

  libphobos_no_cpus := sparc
  libphobos_no_systems := gnu
  ifneq (,$(findstring $(DEB_TARGET_ARCH_CPU),$(libphobos_no_cpus)))
    with_libphobos := disabled for cpu $(DEB_TARGET_ARCH_CPU)
  endif
  ifneq (,$(findstring $(DEB_TARGET_GNU_SYSTEM),$(libphobos_no_systems)))
    with_libphobos := disabled for system $(DEB_TARGET_GNU_SYSTEM)
  endif

  enabled_languages += d
endif

# Fortran 95 -------------------
fortran_no_cross := yes
fortran_no_cross := no

ifneq ($(with_base_only),yes)
  ifneq ($(separate_lang),yes)
    with_fortran := yes
  endif
endif
ifeq ($(fortran_no_cross)-$(DEB_CROSS),yes-yes)
  with_fortran := diasbled for cross compiler package
endif
with_fortran := $(call envfilt, fortran, , , $(with_fortran))

with_fortran := yes

# Build all packages needed for Fortran development
ifeq ($(with_fortran),yes)
  ifeq ($(with_dev),yes)
    with_fdev := yes
  endif
  ifeq ($(with_common_libs),yes)
    with_libgfortran := yes
  endif
  enabled_languages += fortran
endif

# ObjC ------------------------
objc_no_cross := no

ifneq ($(with_base_only),yes)
  ifneq ($(separate_lang),yes)
    with_objc := yes
  endif
endif
ifeq ($(objc_no_cross)-$(DEB_CROSS),yes-yes)
  with_objc := diasbled for cross compiler package
endif
with_objc := $(call envfilt, objc, obj-c++, , $(with_objc))

with_objc := disabled for maemo

ifeq ($(with_objc),yes)
  # the ObjC runtime with garbage collection enabled needs the Boehm GC
  with_objc_gc := yes

  # disable ObjC garbage collection library (needs libgc)
  libgc_no_cpus := avr mips mipsel # alpha amd64 arm armel hppa i386 ia64 m68k mips mipsel powerpc s390 sparc
  libgc_no_systems := knetbsd-gnu
  ifneq (,$(findstring $(DEB_TARGET_ARCH_CPU),$(libgc_no_cpus)))
    with_objc_gc := disabled for cpu $(DEB_TARGET_ARCH_CPU)
  endif
  ifneq (,$(findstring $(DEB_TARGET_GNU_SYSTEM),$(libgc_no_systems)))
    with_objc_gc := disabled for system $(DEB_TARGET_GNU_SYSTEM)
  endif

  # Build all packages needed for Objective-C development
  ifeq ($(with_dev),yes)
    with_objcdev := yes
  endif
  ifeq ($(with_common_libs),yes)
    with_libobjc := yes
  endif

  enabled_languages += objc
endif

# ObjC++ ----------------------
objcxx_no_cross := no

ifneq ($(with_base_only),yes)
  ifneq ($(separate_lang),yes)
    with_objcxx := yes
  endif
endif
ifeq ($(objcxx_no_cross)-$(DEB_CROSS),yes-yes)
  with_objcxx := diasbled for cross compiler package
endif
with_objcxx := $(call envfilt, obj-c++, , c++ objc, $(with_objcxx))

with_objcxx := disabled for maemo

ifeq ($(with_objcxx),yes)
  enabled_languages += obj-c++
endif

# -------------------------------------------------------------------
# other config

# not built from the main source package
ifeq (,$(findstring gcc,$(PKGSOURCE)))
  extra_package := yes
endif

# Don't include docs with GFDL invariant sections
GFDL_INVARIANT_FREE := yes
ifeq ($(distribution),Ubuntu)
  GFDL_INVARIANT_FREE := no
endif
ifeq ($(distribution),maemo)
	GFDL_INVARIANT_FREE := no
endif

with_nls := yes
ifeq ($(PKGSOURCE),gcc-snapshot)
  with_nls := no
endif
with_nls := $(call envfilt, nls, , , $(with_nls))

# powerpc nof libraries -----
with_libnof := no

ifneq (,$(findstring gcc-4,$(PKGSOURCE)))
  with_source := yes
endif
with_source := $(call envfilt, source, , , $(with_source))

# ssp & libssp -------------------------
with_ssp := yes
ssp_no_archs = alpha hppa ia64 m68k mips mipsel
ifneq (, $(filter $(DEB_TARGET_ARCH),$(ssp_no_archs) $(ssp_no_archs:%=uclibc-%)))
  with_ssp := not available on $(DEB_TARGET_ARCH)
endif
with_ssp := $(call envfilt, ssp, , , $(with_ssp))

ifeq ($(with_ssp),yes)
  ifneq ($(distribution),Debian)
    ifneq (,$(findstring gcc-4, $(PKGSOURCE)))
      with_ssp_default := yes
    endif
  endif
endif

# mudflap -------------------
with_mudflap := yes
with_mudflap := $(call envfilt, mudflap, , , $(with_mudflap))

# gomp --------------------
with_gomp := yes
with_gomp := $(call envfilt, gomp, , , $(with_gomp))

# gold --------------------
gold_archs = amd64 armel i386 lpia powerpc sparc
gold_archs = amd64 i386 lpia
ifneq (,$(filter $(DEB_TARGET_ARCH),$(gold_archs)))
  ifneq (,$(findstring snapshot,$(PKGSOURCE))$(findstring 4.5,$(PKGSOURCE)))
    with_gold := yes
  endif
endif

# plugins --------------------
ifneq (,$(findstring snapshot,$(PKGSOURCE))$(findstring 4.5,$(PKGSOURCE)))
  with_plugins := yes
endif

# -------------------------------------------------------------------
# non-extra config
ifeq ($(extra_package),yes)
  ifeq ($(with_separate_libgcj)-$(with_standalone_gcj),yes-no)
    # build stuff
    with_mudflap :=

    # package stuff
    with_gccbase := no
    with_cdev := no
    with_cxx := no
    with_cxxdev := no
  endif
else
  # libssp ------------------
  ifeq ($(with_ssp)-$(with_common_libs),yes-yes)
    #ifneq ($(DEB_CROSS),yes)
      with_libssp := $(if $(wildcard $(builddir)/gcc/auto-host.h), \
                       $(shell if grep -qs '^\#define TARGET_LIBC_PROVIDES_SSP 1' $(builddir)/gcc/auto-host.h; then echo 'libc provides ssp'; else echo 'yes'; fi))
    #endif
  endif

  # libmudflap --------------
  ifeq ($(with_mudflap)-$(with_common_libs),yes-yes)
    ifneq ($(DEB_CROSS),yes)
      with_libmudflap := yes
    endif
  endif

  # libgomp -----------------
  ifeq ($(with_gomp)-$(with_common_libs),yes-yes)
    #ifneq ($(DEB_CROSS),yes)
      with_libgomp := yes
    #endif
  endif

  # protoize, fixincludes -------
  ifneq ($(DEB_CROSS),yes)
    ifeq ($(with_common_pkgs),yes)
      with_proto	:= yes
      with_fixincl	:= yes
    endif
  endif

  # Shared libgcc --------------------
  ifeq ($(with_common_libs),yes)
    with_libgcc := yes
    with_shared_libgcc := yes
  endif

  # libgcc-math --------------------
  with_libgmath := no
  ifneq (,$(findstring i486,$(DEB_TARGET_ARCH)))
    #with_libgccmath := yes
    #with_lib64gmath := yes
    #with_libgmathdev := yes
  endif
  ifeq ($(DEB_TARGET_ARCH),amd64)
    #with_libgccmath := yes
    #with_lib32gmath := yes
    #with_libgmathdev := yes
  endif

  # hppa64 build ----------------
  hppa64_no_snap := no
  ifeq ($(DEB_TARGET_ARCH),hppa)
    ifneq ($(DEB_CROSS),yes)
      with_hppa64 := yes
    endif
  endif
  ifeq ($(hppa64_no_snap)-$(PKGSOURCE),yes-gcc-snapshot)
    with_hppa64 := disabled for snapshot build
  endif
  with_hppa64 := $(call envfilt, hppa64, , , $(with_hppa64))

  # ia6432 build ----------------
  ia6432_no_snap := no
  ifeq ($(DEB_TARGET_ARCH),ia64)
    ifneq ($(DEB_CROSS),yes)
      with_ia6432 := yes
    endif
  endif
  ifeq ($(ia6432_no_snap)-$(PKGSOURCE),yes-gcc-snapshot)
    with_ia6432 := disabled for snapshot build
  endif
  with_ia6432 := disabled
  with_ia6432 := $(call envfilt, ia6432, , , $(with_ia6432))

  # spu build -------------------
  spu_no_snap := no
  ifneq (,$(findstring $(DEB_TARGET_ARCH),powerpc ppc64))
    ifneq ($(DEB_CROSS),yes)
      with_spu := yes
    endif
  endif
  ifeq ($(spu_no_snap)-$(PKGSOURCE),yes-gcc-snapshot)
    with_spu := disabled for snapshot build
  endif
  with_spu := $(call envfilt, spu, , , $(with_spu))

  ifeq ($(with_spu),yes)
    ifneq ($(PKGSOURCE),gcc-snapshot)
      with_spucache := yes
      with_spumea64 := yes
    endif
  endif

  # neon build -------------------
  # FIXME: build as a cross compiler to build on armv4 as well
  ifneq (,$(findstring gcc-4, $(PKGSOURCE)))
    ifeq ($(distribution),Ubuntu)
#      neon_archs = armel
#      ifneq (, $(filter $(DEB_TARGET_ARCH),$(neon_archs)))
#        with_neon = yes
#      endif
    endif
  endif
endif

# run testsuite ---------------
with_check := yes
# if you don't want to run the gcc testsuite, uncomment the next line
#with_check := disabled by hand
ifeq ($(with_base_only),yes)
  with_check := no
endif
ifeq ($(DEB_CROSS),yes)
  with_check := disabled for cross compiler package
endif
ifeq ($(REVERSE_CROSS),yes)
  with_check := disabled for reverse cross build
endif
check_no_cpus := m68k
check_no_systems := gnu
ifneq (,$(findstring $(DEB_TARGET_ARCH_CPU),$(check_no_cpus)))
  with_check := disabled for cpu $(DEB_TARGET_ARCH_CPU)
endif
ifneq (,$(findstring $(DEB_TARGET_GNU_SYSTEM),$(check_no_systems)))
  with_check := disabled for system $(DEB_TARGET_GNU_SYSTEM)
endif
ifeq ($(distribution)-$(DEB_HOST_ARCH),Ubuntu-hppa)
  ifneq ($(PKGSOURCE),gcc-snapshot)
    with_check := disabled, testsuite timeouts with expect
  endif
endif
ifneq (,$(findstring gdc,$(PKGSOURCE)))
  with_check := disabled for D
endif
with_check := $(call envfilt, check, , , $(with_check))
with_check := disabled for maemo
ifdef WITHOUT_CHECK
  with_check := disabled by environment
endif
ifneq ($(findstring nocheck, $(DEB_BUILD_OPTIONS)),)
  with_check := disabled by DEB_BUILD_OPTIONS
endif

# not a dependency on all archs, but if available, use it for the testsuite
ifneq (,$(wildcard /usr/bin/localedef))
  locale_data = generate
endif

all_enabled_languages := $(enabled_languages)
languages_without_lang_opt := c++ objc obj-c++ proto

debian_extra_langs := $(subst obj-c++,objcp,$(debian_extra_langs))
export debian_extra_langs

# multilib
biarch_map := powerpc=powerpc64 sparc=sparc64 s390=s390x \
              powerpc64=powerpc mips=mips64 mipsel=mips64el
biarch_cpu := $(patsubst $(DEB_TARGET_GNU_CPU)=%,%, \
			 $(filter $(DEB_TARGET_GNU_CPU)=%,$(biarch_map)))

biarch64 := no
biarch34 := no
biarchn32 := no
define gen_biarch
  ifneq (yes,$$(call envfilt, biarch, , ,yes))
    biarch$1archs :=
  endif
  ifneq (,$$(findstring /$$(DEB_TARGET_ARCH)/,$$(biarch$1archs)))
    biarch$1 := yes
    #biarch$1subdir = $$(biarch_cpu)-$$(DEB_TARGET_GNU_SYSTEM)
    biarch$1subdir = $1
    ifeq ($$(with_libgcc),yes)
      with_lib$1gcc := yes
    endif
    ifeq ($$(with_libcxx),yes)
      with_lib$1cxx := yes
    endif
    ifeq ($$(with_libcxxdbg),yes)
      with_lib$1cxxdbg := yes
    endif
    ifeq ($$(with_libobjc),yes)
      with_lib$1objc := yes
    endif
    ifeq ($$(with_libgfortran),yes)
      with_lib$1gfortran := yes
    endif
    ifeq ($$(with_libmudflap),yes)
      with_lib$1mudflap := yes
    endif
    ifeq ($$(with_libssp),yes)
      with_lib$1ssp := yes
    endif
    ifeq ($$(with_libgomp),yes)
      with_lib$1gomp:= yes
    endif

    biarch_multidir_names = libiberty libgcc
    ifneq (,$$(findstring gcc-, $$(PKGSOURCE)))
      biarch_multidir_names += libstdc++-v3 libobjc libgfortran libssp \
				libgomp libmudflap zlib
      ifeq ($$(with_objc_gc),yes)
        biarch_multidir_names += boehm-gc
      endif
    endif
    export biarch_multidir_names
    ifneq (,$$(findstring 32,$1))
      TARGET64_MACHINE := $$(strip $$(subst $$(DEB_TARGET_GNU_CPU),$$(biarch_cpu), \
				$$(TARGET_ALIAS)))
      TARGET32_MACHINE := $$(TARGET_ALIAS)
    else
      TARGET64_MACHINE := $$(TARGET_ALIAS)
      TARGET64_MACHINE := $$(strip $$(subst $$(DEB_TARGET_GNU_CPU),$$(biarch_cpu), \
				$$(TARGET_ALIAS)))
    endif
    export TARGET32_MACHINE
    export TARGET64_MACHINE
  endif
endef
biarch32archs := /ppc64/kfreebsd-amd64/
biarch64archs := /powerpc/sparc/s390/mips/mipsel/
biarchn32archs := /mips/mipsel/
$(foreach x,32 64 n32,$(eval $(call gen_biarch,$(x))))

ifneq (,$(filter yes,$(biarch32) $(biarch64) $(biarchn32)))
  multilib := yes
endif

multilib_archs = $(sort $(subst /, , $(biarch64archs) $(biarch32archs) $(biarchn32archs)))

biarchsubdirs := \
  $(if $(filter yes,$(biarch64)),$(biarch64subdir),) \
  $(if $(filter yes,$(biarch32)),$(biarch32subdir),) \
  $(if $(filter yes,$(biarchn32)),$(biarchn32subdir),)
biarchsubdirs := {$(strip $(shell echo $(biarchsubdirs) | tr " " ","))}

#ifeq ($(DEB_TARGET_ARCH),ia64)
#  biarch32 := yes
#endif

ifeq ($(PKGSOURCE),gcc-snapshot)
  no_biarch_libs := yes
endif
ifdef DEB_CROSS_NO_BIARCH
  no_biarch_libs := yes
endif
ifeq ($(with_d)-$(with_separate_gdc),yes-yes)
  no_biarch_libs := yes
endif

no_biarch_libs := yes

ifeq ($(no_biarch_libs),yes)
  with_lib64gcc		:= no
  with_lib64cxx		:= no
  with_lib64objc	:= no
  with_lib64ffi		:= no
  with_lib64gcj		:= no
  with_lib64fortran	:= no
  with_lib64mudflap	:= no
  with_lib64ssp		:= no

  with_lib32gcc		:= no
  with_lib32cxx		:= no
  with_lib32objc	:= no
  with_lib32ffi		:= no
  with_lib32gcj		:= no
  with_lib32fortran	:= no
  with_lib32mudflap	:= no
  with_lib32ssp		:= no

  with_libn32gcc	:= no
  with_libn32cxx	:= no
  with_libn32objc	:= no
  with_libn32ffi	:= no
  with_libn32gcj	:= no
  with_libn32fortran	:= no
  with_libn32mudflap	:= no
  with_libn32ssp	:= no

  ifeq ($(PKGSOURCE),gcc-snapshot)
    #biarch64	:= disabled for snapshot build
    #biarch32	:= disabled for snapshot build
    #biarchn32	:= disabled for snapshot build
    with_java_plugin := no
  endif

  ifdef DEB_CROSS_NO_BIARCH
    biarch64   	:= disabled by DEB_CROSS_NO_BIARCH
    biarch32 	:= disabled by DEB_CROSS_NO_BIARCH
    biarchn32 	:= disabled by DEB_CROSS_NO_BIARCH
  endif

  ifeq ($(with_d)-$(with_separate_gdc),yes-yes)
    biarch64	:= disabled for D
    biarch32	:= disabled for D
    biarchn32	:= disabled for D
  endif

endif

ifeq ($(biarch32),yes)
  with_32bit_check := $(strip $(if $(wildcard build/runcheck.out), \
				$(shell cat build/runcheck.out), \
				$(shell CC="gcc -m32" bash debian/runcheck.sh)))
endif

ifeq ($(biarch64),yes)
  ifneq (,$(filter $(DEB_TARGET_ARCH_CPU),mips mipsel))
    with_64bit_check := $(strip $(if $(wildcard build/runcheck.out), \
			$(shell cat build/runcheck.out), \
			$(shell CC="gcc -mabi=64" bash debian/runcheck.sh)))
  else
    with_64bit_check := $(strip $(if $(wildcard build/runcheck.out), \
			$(shell cat build/runcheck.out), \
			$(shell CC="gcc -m64" bash debian/runcheck.sh)))
  endif
endif

ifeq ($(biarchn32),yes)
  with_n32bit_check := $(strip $(if $(wildcard build/runcheck.out), \
				$(shell cat build/runcheck.out), \
				$(shell CC="gcc -mabi=n32" bash debian/runcheck.sh)))
endif

# GNU locales
force_gnu_locales := yes
locale_no_cpus := m68k
locale_no_systems := knetbsd-gnu
ifneq (,$(findstring $(DEB_TARGET_GNU_SYSTEM),$(locale_no_systems)))
  force_gnu_locales := disabled for system $(DEB_TARGET_GNU_SYSTEM)
endif

gcc_tarpath		:= $(firstword $(wildcard gcc-*.tar.* /usr/src/gcc-$(BASE_VERSION)/gcc-*.tar.*))
gcc_tarball		:= $(notdir $(gcc_tarpath))
gcc_srcdir		:= $(subst -dfsg,,$(patsubst %.tar.xz,%,$(patsubst %.tar.lzma,%,$(patsubst %.tar.gz,%,$(gcc_tarball:.tar.bz2=)))))

ifeq ($(with_pascal),yes)
  gpc_tarpath		:= $(firstword $(wildcard gpc-*.tar.* /usr/src/gcc-$(BASE_VERSION)/gpc-*.tar.*))
  gpc_tarball		:= $(notdir $(gpc_tarpath))
  gpc_srcdir		:= $(patsubst %.tar.xz,%,$(patsubst %.tar.lzma,%,$(patsubst %.tar.gz,%,$(gpc_tarball:.tar.bz2=))))
endif

ifeq ($(with_d),yes)
  gdc_tarpath		:= $(firstword $(wildcard gdc-*.tar.* /usr/src/gcc-$(BASE_VERSION)/gdc-*.tar.*))
  gdc_tarball		:= $(notdir $(gdc_tarpath))
  gdc_srcdir		:= $(patsubst %.tar.xz,%,$(patsubst %.tar.lzma,%,$(patsubst %.tar.gz,%,$(gdc_tarball:.tar.bz2=))))
endif

unpack_stamp		:= $(stampdir)/01-unpack-stamp
pre_patch_stamp		:= $(stampdir)/02-pre-patch-stamp
patch_stamp		:= $(stampdir)/02-patch-stamp
src_spu_stamp		:= $(stampdir)/02-src-spu-stamp
control_stamp		:= $(stampdir)/03-control-stamp
configure_stamp		:= $(stampdir)/04-configure-stamp
build_stamp		:= $(stampdir)/05-build-stamp
build_html_stamp	:= $(stampdir)/05-build-html-stamp
build_locale_stamp	:= $(stampdir)/05-build-locale-stamp
build_doxygen_stamp	:= $(stampdir)/05-build-doxygen-stamp
build_javasrc_stamp	:= $(stampdir)/05-build-javasrc-stamp
build_javadoc_stamp	:= $(stampdir)/05-build-javadoc-stamp
check_stamp		:= $(stampdir)/06-check-stamp
check_inst_stamp	:= $(stampdir)/06-check-inst-stamp
install_stamp		:= $(stampdir)/07-install-stamp
install_snap_stamp	:= $(stampdir)/07-install-snap-stamp
binary_stamp		:= $(stampdir)/08-binary-stamp

configure_dummy_stamp	:= $(stampdir)/04-configure-dummy-stamp
build_dummy_stamp	:= $(stampdir)/05-build-dummy-stamp
install_dummy_stamp	:= $(stampdir)/07-install-dummy-stamp

configure_hppa64_stamp	:= $(stampdir)/04-configure-hppa64-stamp
build_hppa64_stamp	:= $(stampdir)/05-build-hppa64-stamp
install_hppa64_stamp	:= $(stampdir)/07-install-hppa64-stamp

configure_neon_stamp	:= $(stampdir)/04-configure-neon-stamp
build_neon_stamp		:= $(stampdir)/05-build-neon-stamp
install_neon_stamp	:= $(stampdir)/07-install-neon-stamp

configure_ia6432_stamp	:= $(stampdir)/04-configure-ia6432-stamp
build_ia6432_stamp	:= $(stampdir)/05-build-ia6432-stamp
install_ia6432_stamp	:= $(stampdir)/07-install-ia6432-stamp

configure_ia6432_stamp	:= $(stampdir)/04-configure-ia6432-stamp
build_ia6432_stamp	:= $(stampdir)/05-build-ia6432-stamp
install_ia6432_stamp	:= $(stampdir)/07-install-ia6432-stamp

configure_spu_stamp	:= $(stampdir)/04-configure-spu-stamp
build_spu_stamp		:= $(stampdir)/05-build-spu-stamp
install_spu_stamp	:= $(stampdir)/07-install-spu-stamp

control_dependencies	:= $(patch_stamp)

ifeq ($(PKGSOURCE),gcc-snapshot)
    configure_dependencies = $(configure_stamp)
    build_dependencies = $(build_stamp)
    install_dependencies = $(install_snap_stamp)
    ifeq ($(with_check),yes)
      check_dependencies += $(check_stamp)
    endif
else
  ifeq ($(with_base_only),yes)
    configure_dependencies = $(configure_dummy_stamp)
    build_dependencies = $(build_dummy_stamp)
    install_dependencies = $(install_dummy_stamp)
  else
    configure_dependencies = $(configure_stamp)
    build_dependencies = $(build_stamp)
    install_dependencies = $(install_stamp)
    ifeq ($(with_check),yes)
      check_dependencies += $(check_stamp)
    endif
  endif
endif

ifneq (,$(findstring gcj-, $(PKGSOURCE)))
  ifeq ($(with_gcj_base_only),yes)
    configure_dependencies = $(configure_dummy_stamp)
    build_dependencies = $(build_dummy_stamp)
    install_dependencies = $(install_dummy_stamp)
  endif
endif

ifeq ($(with_neon),yes)
  build_dependencies += $(build_neon_stamp)
  install_dependencies += $(install_neon_stamp)
endif

ifeq ($(with_hppa64),yes)
  build_dependencies += $(build_hppa64_stamp)
  ifneq ($(PKGSOURCE),gcc-snapshot)
    install_dependencies += $(install_hppa64_stamp)
  endif
endif

ifeq ($(with_ia6432),yes)
  build_dependencies += $(build_ia6432_stamp)
  ifneq ($(PKGSOURCE),gcc-snapshot)
    install_dependencies += $(install_ia6432_stamp)
  endif
endif

ifeq ($(with_spu),yes)
  control_dependencies += $(src_spu_stamp)
  build_dependencies += $(build_spu_stamp)
  ifneq ($(PKGSOURCE),gcc-snapshot)
    install_dependencies += $(install_spu_stamp)
  endif
endif

build_dependencies += $(check_dependencies)

stamp-dir:
	mkdir -p $(stampdir)
